<template>
  <div class='rescource-list'>
    <el-card class="box-card">
      <div slot="header" class="clearfix">
        <el-form ref="form" :inline="true" :model="form" class="demo-form-inline">
          <el-form-item prop="name" label="资源名称">
            <el-input v-model="form.name" placeholder="资源名称"></el-input>
          </el-form-item>
          <el-form-item prop="url" label="资源路径">
            <el-input v-model="form.url" placeholder="资源路径"></el-input>
          </el-form-item>
          <el-form-item prop="categoryId" label="资源分类">
            <el-select v-model="form.categoryId" placeholder="资源分类" clearable>
              <el-option
                v-for="item in rescourceCategories"
                :key="item.id"
                :label="item.name"
                :value="item.id"
              ></el-option>
            </el-select>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              @click="onSubmit"
              :disabled="isLoading"
            >查询</el-button>
            <el-button
              @click="onRest"
              :disabled="isLoading"
            >重置</el-button>
          </el-form-item>
        </el-form>
      </div>
      <el-table
        :data="rescources"
        v-loading = "isLoading"
        border
        style="width: 100%;margin-bottom:10px">
        <el-table-column
          prop="id"
          label="编号"
          align="center"
          width="80px"
        >
        </el-table-column>
        <el-table-column
          prop="name"
          align="center"
          label="资源名称"
        >
        </el-table-column>
        <el-table-column
          prop="url"
          align="center"
          label="资源路径">
        </el-table-column>
        <el-table-column
          prop="description"
          align="center"
          label="描述">
        </el-table-column>
        <el-table-column
          prop="createdTime"
          align="center"
          label="添加时间">
        </el-table-column>
        <el-table-column
          label="操作"
          align="center"
        >
          <template slot-scope="scope">
            <el-button
              size="mini"
              @click="handleEdit(scope.row)">编辑</el-button>
            <el-button
              size="mini"
              type="danger"
              @click="handleDelete(scope.row)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        :disabled="isLoading"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :current-page.sync="form.current"
        :page-sizes="[5, 10, 15, 20]"
        :page-size="form.size"
        layout="total, sizes, prev, pager, next, jumper"
        :total="totalCount">
      </el-pagination>
    </el-card>
  </div>
</template>

<script lang='ts'>
import Vue from 'vue'
import { getRescourcePages, getCategoryAll } from '@/services/rescource'
import { Form } from 'element-ui'
export default Vue.extend({
  name: 'rescourcelist',
  data () {
    return {
      form: {
        name: '',
        url: '',
        current: 1,
        size: 5,
        categoryId: null
      },
      rescources: [],
      totalCount: 0,
      rescourceCategories: [],
      isLoading: false
    }
  },
  created () {
    this.loadRescources()
    this.loadCategoryAll()
  },
  methods: {
    async loadCategoryAll () {
      const { data } = await getCategoryAll()
      this.rescourceCategories = data.data
    },
    async loadRescources () {
      this.isLoading = true
      const { data } = await getRescourcePages(this.form)
      if (data.code === '000000') {
        this.rescources = data.data.records
        this.totalCount = data.data.total
      }
      this.isLoading = false
    },
    onSubmit () {
      this.form.current = 1
      this.loadRescources()
    },
    handleEdit (item: any) {
      console.log(item)
    },
    handleDelete (item: any) {
      console.log(item)
    },
    handleSizeChange (val: number) {
      this.form.size = val
      this.form.current = 1
      this.loadRescources()
    },
    handleCurrentChange (val: number) {
      this.form.current = val
      this.loadRescources()
    },
    onRest () {
      (this.$refs.form as Form).resetFields()
      this.form.current = 1
      this.loadRescources()
    }
  }
})
</script>

<style lang='scss' scoped></style>
